package cn.itlaobing.springbootdemo.service.impl;

import cn.itlaobing.springbootdemo.entity.Student;
import cn.itlaobing.springbootdemo.entity.SystemAccount;
import cn.itlaobing.springbootdemo.reponsitory.SystemAccountRepository;
import cn.itlaobing.springbootdemo.service.SystemAccountService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

@Service
public class SystemAccoutServiceImpl implements SystemAccountService {
    Log log= LogFactory.getLog(this.getClass());
    @Autowired
   private SystemAccountRepository systemAccountRepository;

    @Override
    public SystemAccount authentication(String account, String password) {
        SystemAccount systemAccount=systemAccountRepository.findByAccount(account);

        if(systemAccount==null){
            return null;
        }
        //比较密码
        String pwdDigest=getPasswordDigest(password,systemAccount.getSalt());
        if(pwdDigest.equals(systemAccount.getPasswordDigest())){
            return systemAccount;
        }
        return null;
    }

    @Override
    public SystemAccount save(String account, String password, Student student) {
        SystemAccount systemAccount=new SystemAccount();
        String salt= RandomStringUtils.random(32,true,true); //产生32 位的随机盐值
        systemAccount.setSalt(salt);
        systemAccount.setPasswordDigest(getPasswordDigest(password,salt));
        systemAccount.setAccount(account);
        systemAccount.setStudent(student);
        systemAccountRepository.save(systemAccount);
        return systemAccount;
    }

    @Override
    public SystemAccount chagePassword(String account, String newPassword) {
        SystemAccount systemAccount=systemAccountRepository.findByAccount(account);
        String salt= RandomStringUtils.random(32,true,true); //产生32 位的随机盐值
        systemAccount.setSalt(salt);
        systemAccount.setPasswordDigest(getPasswordDigest(newPassword,salt));
        systemAccountRepository.saveAndFlush(systemAccount);
        return systemAccount;
    }

    @Override
    public void updatePicHead(SystemAccount systemAccount) {
        SystemAccount sysAccount=systemAccountRepository.findOne(systemAccount.getId());
        sysAccount.setPicHead(systemAccount.getPicHead());
        systemAccountRepository.save(sysAccount);
    }

    @Override
    public Page<SystemAccount> findByStudentNameOrAccount(String nameOrAccount, Pageable pageable) {
        if(StringUtils.isBlank(nameOrAccount)){
            return systemAccountRepository.findAll(pageable);
        }
        return systemAccountRepository.findByStudentNameOrAccount(nameOrAccount,pageable);

    }


    /**
     * 使用盐值加密
     * @param password
     * @param salt
     * @return
     */
    private String getPasswordDigest(String password,String salt){
        MessageDigest messageDigest= DigestUtils.getDigest(MessageDigestAlgorithms.SHA_1);
        try {
            messageDigest.update(salt.getBytes("UTF-8"));
            byte[] result= messageDigest.digest(password.getBytes("UTF-8"));
            //将加密后的结果转换为 16进制的字符串
            return DigestUtils.sha1Hex(result);

        } catch (UnsupportedEncodingException e) {

        }
        return "";
    }
}
